//
//  ZZCAgentStatusController.m
//  ZZC-Finance
//
//  Created by a on 2017/8/18.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "ZZCAgentStatusController.h"
#import "ZZCAgentController.h"
#import "ZZCAgentView.h"

@interface ZZCAgentStatusController ()<UITextFieldDelegate,UIScrollViewDelegate>
@property (strong, nonatomic) ZZCAgentView *agentView;
@property (strong, nonatomic) ZZCMenuView *menuViewA;
@property (strong, nonatomic) ZZCMenuView *menuViewB;
@property (strong, nonatomic) ZZCMenuView *menuViewC;
@property (copy, nonatomic) NSString *enterpriseName;

@end

@implementation ZZCAgentStatusController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"代理商";
    [self messageShow];
    
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.agentView];
    [self.agentView.rankBtn addTarget:self action:@selector(rankBtnClick) forControlEvents:UIControlEventTouchUpInside];
    
    [self.agentView.regionBtn addTarget:self action:@selector(regionBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.agentView.natureBtn addTarget:self action:@selector(natureBtnClick) forControlEvents:UIControlEventTouchUpInside];
    self.agentView.bgScrollView.delegate = self;
    
    UIView *view = [[UIView alloc] initWithFrame:_agentView.bgScrollView.bounds];
    view.backgroundColor = [UIColor colorWithHex:0x000000 alpha:0.1];
    [self.agentView.bgScrollView addSubview:view];
    //代理等级
    NSArray *arr = @[@"省级",@"市级",@"县级"];
    self.menuViewA = [[ZZCMenuView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.agentView.rankView.frame), ZZCScreenW, 132) contentArray:arr];
    __weak typeof (self) weakself = self;
    self.menuViewA.block = ^(BOOL isSeletct ,NSString *title){
        weakself.menuViewA.hidden = YES;
        if (title.length != 0) {
            [weakself.agentView.rankBtn setTitle:title forState:UIControlStateNormal];
        }
    };
    
    //企业性质
    NSArray *arr1 = @[@"国有",@"民企",@"外国"];
    self.menuViewB = [[ZZCMenuView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.agentView.natureView.frame), ZZCScreenW, 132) contentArray:arr1];
    
    self.menuViewB.block = ^(BOOL isSeletct ,NSString *title){
        weakself.menuViewB.hidden = YES;
        if (title.length != 0) {
            [weakself.agentView.natureBtn setTitle:title forState:UIControlStateNormal];
        }
    };
    
    //代理区域
    NSArray *arr2 = @[@"城市",@"郊区",@"农村",@"山区"];
    self.menuViewC = [[ZZCMenuView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.agentView.regionView.frame), ZZCScreenW, 132) contentArray:arr2];
    
    self.menuViewC.block = ^(BOOL isSeletct ,NSString *title){
        weakself.menuViewC.hidden = YES;
        if (title.length != 0) {
            [weakself.agentView.regionBtn setTitle:title forState:UIControlStateNormal];
        }
    };
    self.menuViewA.hidden = YES;
    self.menuViewB.hidden = YES;
    self.menuViewC.hidden = YES;
    [self.agentView addSubview:self.menuViewA];
    self.menuViewA.y = self.agentView.rankView.y+44;
    [self.agentView addSubview:self.menuViewB];
    self.menuViewB.y = self.agentView.natureView.y+44;
    [self.agentView addSubview:self.menuViewC];
    self.menuViewC.y = self.agentView.regionView.y+44;
    
    [self bottomBtn];
    
}


- (void)bottomBtn {
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, ZZCScreenH-113, ZZCScreenW, 49)];
    btn.backgroundColor = [UIColor colorWithHex:0xEA6E25];
    [btn setTitle:@"重新申请" forState:0];
    [btn addTarget:self action:@selector(nextClick) forControlEvents:UIControlEventTouchUpInside];
    btn.titleLabel.font = FONT(19);
    [btn setTag: 1121];
    [self.view addSubview:btn];
}
- (void)nextClick {
    ZZCLog(@"下一步");
//    ZZCScanController *scan = [ZZCScanController new];
//    [self.navigationController pushViewController:scan animated:YES];
    ZZCAgentController *age = [ZZCAgentController new];
    [self.navigationController pushViewController:age animated:YES];
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    self.menuViewA.y = self.agentView.rankView.y+44 - scrollView.contentOffset.y;
    self.menuViewB.y = self.agentView.natureView.y+44 - scrollView.contentOffset.y;
    self.menuViewC.y = self.agentView.regionView.y+44 - scrollView.contentOffset.y;
}


- (void)rankBtnClick {
    [self.view endEditing:YES];
    self.menuViewA.hidden = !self.menuViewA.hidden;
    self.menuViewB.hidden = YES;
    self.menuViewC.hidden = YES;
}
- (void)natureBtnClick {
    [self.view endEditing:YES];
    self.menuViewB.hidden = !self.menuViewB.hidden;
    self.menuViewA.hidden = YES;
    self.menuViewC.hidden = YES;
    
}
- (void)regionBtnClick {
    [self.view endEditing:YES];
    self.menuViewC.hidden = !self.menuViewC.hidden;
    self.menuViewA.hidden = YES;
    self.menuViewB.hidden = YES;
}
#pragma mark - 值接收
- (void)textFieldDidBeginEditing:(UITextField *)textField {
    self.menuViewA.hidden = YES;
    self.menuViewB.hidden = YES;
    self.menuViewC.hidden = YES;
}
- (void)textFieldDidEndEditing:(UITextField *)textField {
    if (textField == self.agentView.enterpriseNameTextField) {
        //企业名字值接收,自己创建属性接收需要的值
        self.enterpriseName = textField.text;
        ZZCLog(@"%@",textField.text);
    }else if (textField == self.agentView.enterpriseLegalTextField){
        ZZCLog(@"%@",textField.text);
    }else if (textField == self.agentView.enterprisePhoneTextField){
        ZZCLog(@"%@",textField.text);
    }else if (textField == self.agentView.legalMailboxTextField){
        ZZCLog(@"%@",textField.text);
    }else if (textField == self.agentView.enterpriseAddressTextField){
        ZZCLog(@"%@",textField.text);
    }
}
-(void)messageShow {
    NSString *token = ZZCNSUserDefaults(@"token");
    NSDate *currentDate = [NSDate date];
    NSString *timeStr = [NSString stringWithFormat:@"%ld",(long)[currentDate timeIntervalSince1970]];
    NSDictionary *dict = @{
                           @"token":token,
                           @"type":@(3),
                           };
    NSString *sign = [ZZCRequest getSignWithDict:dict];
    sign = [NSString md5:[sign lowercaseString]];
    NSDictionary *param = @{
                            @"timestamp":timeStr,
                            @"sign":sign,
                            @"token":token,
                            @"type":@(3)
                            };
    NSLog(@"%@",param);
    [[ZZCRequest sharedNetworkTools] POST:ZZCURL_API_AREA parameters:param finished:^(id  _Nullable responseObject, NSError * _Nullable error) {
        //        NSInteger code = [responseObject[@"code"] integerValue];
        id X = responseObject;
        NSLog(@"%@",X);
        NSString *levelStr;
        if ([X[@"data"][@"level"]  isEqual: @(1)]) {
            levelStr = @"省级";
        } else if ([X[@"data"][@"level"]  isEqual: @(2)]) {
            levelStr = @"市级";
        } else if ([X[@"data"][@"level"]  isEqual: @(3)]) {
            levelStr = @"县级";
        }
        _agentView.enterpriseNameTextField.text = X[@"data"][@"company_name"];
        _agentView.natureBtn.titleLabel.text = X[@"data"][@"type"];
        _agentView.enterpriseLegalTextField.text = X[@"data"][@"corporation"];
        _agentView.enterprisePhoneTextField.text = X[@"data"][@"phone"];
        _agentView.legalMailboxTextField.text = X[@"data"][@"email"];
        _agentView.enterpriseAddressTextField.text = X[@"data"][@"address"];
        _agentView.rankBtn.titleLabel.text = levelStr;
        _agentView.regionLabel.text = [NSString stringWithFormat:@"%@-%@-%@",X[@"data"][@"province"],X[@"data"][@"city"],X[@"data"][@"district"]];

        [_agentView.licenseImageV sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",ZZCURL_API,X[@"data"][@"license"]]] placeholderImage:nil];
        [_agentView.accreditImageV sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",ZZCURL_API,X[@"data"][@"proxy"]]] placeholderImage:nil];

        
        UIView *subView = [self.view viewWithTag:1447];
        UIButton *button = [self.view viewWithTag:1121];
        if ([[X[@"data"][@"status"] stringValue] isEqualToString:@"0"]) {
//            button.hidden = YES;
            _agentView.statusLabel.text = @"   正在审核中";
            button.hidden = YES;
        } else if ([[X[@"data"][@"status"] stringValue] isEqualToString:@"1"]) {
            _agentView.statusLabel.text = @"   已通过审核";
            button.hidden = YES;
        } else if ([[X[@"data"][@"status"] stringValue] isEqualToString:@"-2"]) {
            _agentView.statusLabel.text = [NSString stringWithFormat:@"   未通过审核: %@",X[@"data"][@"remark"]];
            //            _stateLabel.height = ZZCScreenH * 0.108f;
            subView.height = _agentView.statusLabel.height - 49;
            button.hidden = NO;
        }
        //        NSLog(@"%@",responseObject[@"data"][@"status"]);
    }];
}
- (ZZCAgentView *)agentView {
    if (_agentView == nil) {
        _agentView = [ZZCAgentView agentView];
        _agentView.frame = CGRectMake(0, 0, ZZCScreenW, ZZCScreenH);
        _agentView.enterpriseNameTextField.delegate = self;
        _agentView.enterpriseLegalTextField.delegate = self;
        _agentView.enterprisePhoneTextField.delegate = self;
        _agentView.legalMailboxTextField.delegate = self;
        _agentView.enterpriseAddressTextField.delegate = self;
        
    }
    return _agentView;
}
@end
